package com.buba.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.buba.pojo.record.DrugRecord;
import com.buba.pojo.record.Record;
import com.buba.pojo.user.PatientCard;
import com.buba.user.feign.PayFeign;
import com.buba.user.mapper.PatientCardMapper;
import com.buba.user.pojoVo.PayVo;
import com.buba.utils.R;
import lombok.AllArgsConstructor;
import org.apache.commons.lang.math.NumberUtils;
import com.buba.user.service.PatientCardService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

import java.util.Calendar;
import java.util.Date;
/**
 * <p>
 * 就诊卡表 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2022-06-09
 */
@Service
public class PatientCardServiceImpl extends ServiceImpl<PatientCardMapper, PatientCard> implements PatientCardService {

    @Autowired
    private PayFeign payFeign;

    @Autowired
    private PatientCardMapper patientCardMapper;


    @Override
    public R pay(PayVo p) {
        //先根据就诊卡卡号查询就诊卡是否存在
        PatientCard patientCard = this.getOne(new QueryWrapper<PatientCard>().eq("card_no",p.getHospitalNumber()));
        if (patientCard == null){
            return R.error().data("message","充值失败！");
        }
        patientCard.setBalance(patientCard.getBalance().add(p.getMoney()));

        //然后往记录表里添加充值记录
        Record record=new Record();
        record.setPatientcardDi(patientCard.getId());
        record.setPatientId(p.getPatientId());
        record.setMoney(p.getMoney());
        record.setRechargeExplain(p.getRechargeExplain());
        Calendar calendar = Calendar.getInstance();//获取时间
        record.setCreationTime(calendar.getTime());
        record.setModificationTime(calendar.getTime());
        //使用openFeign 调用 Mr_record 服务里面的接口 往记录表里添加充值记录
        R r = payFeign.paytoRecord(record);
        if (r.getCode() != 20000){
            return r.data("message","充值失败！");
        }

        patientCard.setModificationTime(calendar.getTime());
        //最后修改就诊卡里面的金额
        boolean update = this.updateById(patientCard);
        if (!update){
            return R.error().data("message","充值失败！");
        }
        return R.ok().data("message","充值成功！").data("record_id",r.getData().get("record_id"));
    }


    //根据就诊卡号查询就诊卡还多少钱
    @Override
    public R getBlaceByhospitalNumber(String hospitalNumber) {
        PatientCard patientCard = this.getOne(new QueryWrapper<PatientCard>().eq("card_no",hospitalNumber));
        return R.ok().data("patientCard",patientCard);
    }

    @Override
    public Boolean payment(String cardNo, String price) {
        //根据就诊卡查询余额
        QueryWrapper<PatientCard> wrapper = new QueryWrapper<>();
        wrapper.eq("card_no" , cardNo);
        PatientCard patientCard = baseMapper.selectOne(wrapper);
        patientCard.setBalance(patientCard.getBalance().subtract(NumberUtils.createBigDecimal(price)));
        int i = baseMapper.updateById(patientCard);
        if (i!=0){
            return true;
        }
        return false;
    }

    @Override
    public int zhifu(String cardNo, BigDecimal registration) {
        return patientCardMapper.zhifu(cardNo,registration);
    }
}
